//
//  DBMapViewController.m
//  MAP
//
//  Created by 庞雷 on 15/7/19.
//  Copyright (c) 2015年 haha. All rights reserved.
//

#import "DBMapViewController.h"

@interface DBMapViewController ()

@end

@implementation DBMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    
// 1.要使用百度地图,请先启动 BaiduMapManager
    _mapManager = [[BMKMapManager alloc] init];
    // 如果需要关注网络及授权验证事件,请设定 generalDelegate 参数为 self
    // 如果不需要关注网络及授权验证事件,请设定 generalDelegate 参数为 nil
    BOOL ret = [_mapManager start:@"X8DDxdPbClVYeyPgvTiweKay" generalDelegate:self];
    if (!ret) {
        NSLog(@"manager start failed!");
    }
    
    // 代码创建 BMKMapView
    _mapView = [[BMKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds]; // 将创建的 BMKMapView 添加到 View 上
                                                                                // [self.view addSubview:_mapView];
    
//2.定位功能
  
    //2.1初始化BMKLocationService 并初步定位
    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    //启动LocationService
    [_locService startUserLocationService];
    _mapView.showsUserLocation = YES;//显示定位图层

    //2.2定位采用罗盘状态
   _mapView.showsUserLocation = NO;//先关闭显示的定位图层
    _mapView.userTrackingMode = BMKUserTrackingModeFollowWithHeading;//罗盘
    _mapView.showsUserLocation = YES;//显示定位图层

    //3.显示附近农户、农机、产业商信息
    
    [self addPointAnnotation];
    
    
    
  [self.view addSubview:_mapView];   
    
}

-(void)addPointAnnotation{
    
    BMKPointAnnotation *pointAnnotation = [[BMKPointAnnotation alloc] init];
    CLLocationCoordinate2D coor;
    coor.latitude = 39.015;
    
    coor.longitude = 116.404;
    
    NSLog(@"%f",coor.longitude);
    
    
    pointAnnotation.coordinate = coor;
    
    pointAnnotation.title = @"test";
    
    pointAnnotation.subtitle = @"此Annotation可拖拽!";
    
    [_mapView addAnnotation:pointAnnotation];
    
    
    
    
    
    BMKPointAnnotation* pointAnnotation1 = [[BMKPointAnnotation alloc]init];
    
    CLLocationCoordinate2D coor1;
    
    coor1.latitude = 39.917;
    
    coor1.longitude = 116.454;
    
    pointAnnotation1.coordinate = coor1;
    
    pointAnnotation1.title = @"test1";
    
    pointAnnotation1.subtitle = @"此Annotation可拖拽!";
    
    [_mapView addAnnotation:pointAnnotation1];

    
}


















//4.定位事件监听
/**
 *在地图 View 将要启动定位时,会调用此函数 *@param mapView 地图 View
 */


/**
 *在地图View将要启动定位时，会调用此函数
 *@param mapView 地图View
 */
- (void)willStartLocatingUser
{
    NSLog(@"start locate");
}

/**
 *用户方向更新后，会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
    [_mapView updateLocationData:userLocation];
    NSLog(@"heading is %@",userLocation.heading);
}

/**
 *用户位置更新后，会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    //    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    [_mapView updateLocationData:userLocation];
}

/**
 *在地图View停止定位后，会调用此函数
 *@param mapView 地图View
 */
- (void)didStopLocatingUser
{
    NSLog(@"stop locate");
}

/**
 *定位失败后，会调用此函数
 *@param mapView 地图View
 *@param error 错误号，参考CLError.h中定义的错误号
 */
- (void)didFailToLocateUserWithError:(NSError *)error
{
    NSLog(@"location error");
}









- (void)onGetNetworkState:(int)iError {
    if (0 == iError) { NSLog(@"联网成功");
    } else{
        NSLog(@"onGetNetworkState %d",iError); }
}
- (void)onGetPermissionState:(int)iError {
    if (0 == iError) { NSLog(@"授权成功");
    } else {
        NSLog(@"onGetPermissionState %d",iError); }
}

-(void)viewWillAppear:(BOOL)animated {
    [_mapView viewWillAppear];
    _mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
    _locService.delegate = self;
}

-(void)viewWillDisappear:(BOOL)animated {
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; // 不用时，置nil
    _locService.delegate = nil;
}


@end
